package com.vanysoft.business.system.operateLogs.controller;

import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.system.operateLogs.service.SystemApiAccessLogsService;
import com.vanysoft.common.utils.Excel.ExcelUtil;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名称：OperateLogsAction
 * 类描述：日志操作
 * 创建人：X
 * 创建时间：2017年9月11日下午8:52:34
 * 修改人：X
 * 修改时间：2017年9月11日
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/system/operateLogs")
public class SystemApiAccessLogsAction extends BaseController {

    @Autowired
    private SystemApiAccessLogsService systemApiAccessLogsService;

    @RequestMapping(value = "/queryLogsList", method = RequestMethod.GET)
    public JSONResponse queryLogsList(@SessionUser UserInfo userInfo, String scrollPageSize, String scrollPageIndex, String Timevalue, String searchValue, String value1) throws NumberFormatException, VanySoftBaseException {
        Integer _pageIndex = StringUtil.isEmpty(scrollPageIndex) ? 1 : Integer.valueOf(scrollPageIndex);
        Integer _pageSize = StringUtil.isEmpty(scrollPageSize) ? 10 : Integer.valueOf(scrollPageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            map = systemApiAccessLogsService.querySystemApiAccessLogs(_pageSize, _pageIndex, Timevalue, searchValue, value1);
        }
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    /**
     * Function Name               loginValidate
     *
     * @param
     * @return
     * @throws VanySoftBaseException
     * @description 导出操作日志
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年11月18日下午11:33:10          KING          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/exportLogsList", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public void exportLogsList(HttpServletResponse response, String Timevalue, String searchValue, String value1) throws InvalidFormatException, VanySoftBaseException {
        try {
            List<Map<String, Object>> sheetList = new ArrayList<Map<String, Object>>();
            Map<String, Object> sheet1 = new HashMap<String, Object>();

            String[] header1 = {"登录账号", "操作用户", "操作时间", "操作结果", "IP", "接口", "内容"};
            String[] rowkey1 = {"userId", "userName", "accessTime", "rspCode", "loginIp", "apiId", "operationContent"};

            sheet1.put("title", "后台操作日志");
            sheet1.put("rows", systemApiAccessLogsService.queryLogsForExcel(Timevalue, searchValue, value1));
            sheet1.put("rowkey", rowkey1);
            sheet1.put("header", header1);
            sheetList.add(sheet1);

            Workbook workbook = ExcelUtil.exportExample(sheetList);
            String fileName = URLEncoder.encode("操作日志", "UTF-8");

            // 清空response
            response.reset();
            // 设置response的Header
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setContentType("text/html;charset=UTF-8");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            workbook.write(toClient);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
